'use strict';

/**
 * @ngdoc filter
 * @name jgappApp.filter:NBFmt
 * @function
 * @description
 * # NBFmt
 * Filter in the jgappApp.
 */
angular.module('jgappApp')
    .filter('NBFmt', function() {
        return function(input, n) {
            n = n > 0 && n <= 20 ? n : 2;
            if (input) {
                input = parseFloat((input + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
                var l = input.split(".")[0].split("").reverse(),
                    r = input.split(".")[1];
                var t = "";
                for (var i = 0; i < l.length; i++) {
                    t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
                }
                return t.split("").reverse().join("") + "." + r;
            } else {
                return input
            }
        };
    });
